home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl
-
- # Subroutine for decoding form data
-
- sub decodeData
- {
- local(*queryString) = @_ if @_;
-
- #convert pluses to spaces
-
- $queryString =~ s/\+/ /g;
-
- # Convert the hex codes
- #
- # First find them with s/%(..)//ge,
- # then turn the found hexcode into a decimal number,
- # then pack the decimal number into character form,
- # then do normal substitution.
-
- $queryString =~ s/%(..)/pack("c",hex($1))/ge;
-
- # Return 1 for success
-
- return 1;
- }
-
- # Subroutine for interpreting form data
-
- sub parseData
- {
- local(*queryString,*formData) = @_ if @_;
-
- local($key,$value,$curString,@tmpArray,$aName);
-
- # Split the string into key-value pairs, using the '&' character
-
- @tmpArray = split(/&/,$queryString);
-
- # Loop over each pair found
-
- foreach $curString (@tmpArray)
- {
- # Split the key and value, using the '=' character
-
- ($key,$value) = split(/=/,$curString);
-
- # Decode the key and value
-
- &decodeData(*key);
- &decodeData(*value);
-
- # Add the keys and values to the dictionary
- #
- # We will store multple values under a new name,
- # as a string, using the format, value1\376value2...
- # Where \376 is a character unlikely to appear in the
- # values.
-
- if($formData{$key}) # See if this is a multiple value
- {
- $aName = "A_".$key; # Make a new key
-
- if($formData{$aName}) #Check if the array already exists
- {
- $formData{$aName} .= "\376";
- $formData{$aName} .= $value;
-
- # Also put the newest value in the dictionary
- # at the real key.
-
- $formData{$key} = $value;
-
- }
- else #If not, create it and add the current value to the array
- {
- # Add the 1st value for the key to the string
- $formData{$aName} = $formData{$key};
-
- # Add the one that we just found
-
- $formData{$aName} .= "\376";
- $formData{$aName} .= $value;
-
- # Also put the newest value in the dictionary
- # at the real key.
-
- $formData{$key} = $value;
- }
- }
- else # Just add it
- {
- $formData{$key} = $value;
- }
- }
-
- return 1;
- }
-
- # Subroutine for reading post data
-
- sub readPostData
- {
- local(*queryString) = @_ if @_;
-
- local($contentLength);
-
- # Read the environment variable CONTENT_LENGTH
-
- $contentLength = $ENV{"CONTENT_LENGTH"};
-
- # Make sure that there is data to read
-
- if($contentLength)
- {
- # Read contentLength characters from STDIN into queryString
-
- read(STDIN,$queryString,$contentLength);
- }
-
- # Return 1 for success
-
- return 1;
- }
-
- sub readGetData
- {
- local(*queryString) = @_ if @_;
-
- # Read the environment variable QUERY_STRING
-
- $queryString = $ENV{"QUERY_STRING"};
-
- return 1;
- }
-
- sub readData
- {
- local(*queryString) = @_ if @_;
-
- # Read the envorinmental variable REQUEST_METHOD
-
- $requestType = $ENV{"REQUEST_METHOD"};
-
- # If the request is GET use readGetData
- # otherwise, if the request is POST use readPostData
-
- if($requestType eq "GET")
- {
- &readGetData(*queryString);
- }
- elsif($requestType eq "POST")
- {
- &readPostData(*queryString);
- }
-
- }
-
- # Print the header required for all CGI scripts that output dynamic text data
-
- print "Content-type: text/plain\n\n";
-
- print "The form data is:\n\n";
-
- # Make sure that this is a post request
-
- %dataDict = ();
-
- # Call readData, to determine the request type and read the data.
- # Notice that we use the variable name, not its value as an arguement
-
- &readData(*data);
- &parseData(*data,*dataDict);
-
- while(($key,$value)=each(%dataDict))
- {
- if($key =~ /^A_/)
- {
- print "Found a key with multiple values:\n";
-
- @mValues = split(/\376/,$value);
-
- $realKey = $key;
- $realKey =~ s/^A_//;
-
- foreach $mValue (@mValues)
- {
- print "\t",$realKey," = ",$mValue,"\n";
-
- }
- }
- else
- {
- print $key," = ",$value,"\n";
- }
- }
-
-